//角色加菜单
(function(w){
    var roleMenu = function(){
        this.init();
    };
    roleMenu.prototype = {
        init: function(){
            //this.paint();
            this.evt();
        },
        paint: function(obj,cl){
            var html = "",targetId = "",id = "",random = 0,menuData = JSON.parse(sessionStorage.getItem("leftMenuData"));
            obj.innerHTML = "";
            menuData.forEach(function(item,index){
                random = new Date().getTime();
                targetId = "#role"+cl+item.sys_menuID;
                id = "role"+cl+item.sys_menuID;
                if(item.parentID == 0){
                    if(item.menuOrder == 0){
                        html = "<li><input type='checkbox' class = 'role-menu-all'>全部</li><li><input type='checkbox' class = 'role-menu-item' data-menu-id='"+item.sys_menuID+"'><span class='"+item.menuClass+"'></span> "+item.menuName+"</li>";
                    }else{
                        html = "<li><input type='checkbox' class = 'role-menu-items' data-menu-id='"+item.sys_menuID+"'><div data-am-collapse={target:'"+targetId+"'}><span class='"+item.menuClass+"'></span> "+item.menuName+"<span class='am-icon-angle-right am-fr am-margin-right'></span></div>"+
                            "<ul class='am-list admin-sidebar-sub am-collapse' id="+id+"></ul></li>";
                    }
                    obj.innerHTML += html;
                }else{
                    if(item.meuType == 1){
                        html = "<li><input type='checkbox' class = 'role-menu-items' data-menu-id='"+item.sys_menuID+"'><div data-am-collapse={target:'"+targetId+"'}><span class='"+item.menuClass+"'></span> "+item.menuName+"<span class='am-icon-angle-right am-fr am-margin-right'></span></div>"+
                            "<ul class='am-list admin-sidebar-sub am-collapse' id="+id+"></ul></li>";
                        $("#role"+cl+item.parentID).append(html);
                    }else{
                        html = "<li><input type='checkbox' class = 'role-menu-item' data-menu-id='"+item.sys_menuID+"'><span class='"+item.menuClass+"'></span> "+item.menuName+"</li>";
                        $("#role"+cl+item.parentID).append(html);
                    }
                }
            });
        },
        evt: function(){
            $(document).on("click",".role-menu-items",function(){
                if(this.checked)
                    $(this.parentNode).find(".role-menu-item").prop("checked",true);
                else
                    $(this.parentNode).find(".role-menu-item").prop("checked",false);
            });
            $(document).on("click",".role-menu-item",function(){
                if(!this.checked){
                    var checkedObj = $(this.parentNode.parentNode.parentNode).find(".role-menu-item"),length = 0;
                    for(var i = 0;i < checkedObj.length;i++){
                        if(checkedObj[i].checked == true)length++;
                    }
                    if(length == 0){
                        this.parentNode.parentNode.parentNode.getElementsByClassName("role-menu-items")[0].checked = false;
                    }
                }else{
                    if(this.dataset.menuId == "12")return;
                    this.parentNode.parentNode.parentNode.getElementsByClassName("role-menu-items")[0].checked = true;
                }
            });
            $(document).on("click",".role-menu-all",function(){
                if(this.checked)$(this.parentNode.parentNode).find("input").prop("checked",true);else $(this.parentNode.parentNode).find("input").prop("checked",false);
            });
        }
    };
    gObj.roleMenu = roleMenu;
}(window));